// pages/article/article.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,
    modalName:'',
    nofollow_parent_id:'',
    login:''

  },
    /**
   * 关注
   * @param {*} e
   */
  follow:function(e){
    var data = e.target.dataset, parent_id =data.parent_id,child_id = data.child_id,index = data.index;
    var that = this;
    var row = 'list['+index+'].follow';
    util.request('follow/add', {type:1,parent_id:parent_id,child_id:child_id}, function (data) {
      console.log(data);
      if(data.code == 1){
       that.loadData();
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 取消关注
   * @param {*} e
   */
  nofollow:function(e){
    var parent_id = this.data.nofollow_parent_id;
    var that = this;
    util.request('follow/delete', {type:1,parent_id:parent_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          nofollow_parent_id:''
        })
        that.loadData();
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        that.setData({
          nofollow_parent_id:''
        })
        util.fail(data.msg,that);
      }
    });
  },

  showModal:function(e){
    this.setData({
      modalName: 'modal1',
      nofollow_parent_id:e.target.dataset.parent_id
    })
  },

  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },

  fabu:function(){
    this.setData({
      modalName: 'bottomModal',
    })
  },

  loadData:function(){
    var that = this;
    that.setData({
      tips:{
        no_data:false,
        show_loading:true
      }
    });

    util.request('index/index', {}, function (data) {
      that.setData({
        login:app.globalData.login
      })
      if(data.code == 1){
        that.setData({
          list:data.data.list,
          child:data.data.child,
          log:data.data.log,
          has_child:data.data.has_child,
          has_log:data.data.has_log,
          message_count:data.data.message_count,
        });
        that.setData({
          tips:{
            no_data:true,
            show_loading:false
          }
        });
      }else{
        that.setData({
          tips:{
            no_data:false,
            show_loading:false
          }
        });
        util.fail(data.msg,that);
      }
    });
  },

  ViewImage(e) {
    var array = this.data.list[e.currentTarget.dataset.index].image_array,arr = [];
    for(var i = 0;i<array.length;i++){
      arr[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: arr,
      current: e.currentTarget.dataset.url
    });
  },
    /**
   * 收藏/取消收藏
   * @param {*} e
   */
  collect:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.list[index],that = this;
    var collect = data.collect;
    util.request('collect/save', {related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'list['+index+'].collect';
        if(collect.is_collect > 0){
          that.setData({
              [row]:{
                count:collect.count-1,
                is_collect:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:collect.count+1,
              is_collect:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.list[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:3,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'list['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.hideModal();
    this.loadData();

    this.setData({
      login:app.globalData.login
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
